package com.cim.config;

import com.cim.auth.MyInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

/**
 * Created by Administrator on 2017/8/1.
 */
@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {
    /**
     * 重写父类的方法,配置需要拦截的请求
     * @param registry
     */
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new MyInterceptor())
                .addPathPatterns("/api/auth/info")              //获取当前用户信息
                .addPathPatterns("/api/auth/modifi")            //修改
                .addPathPatterns("/api/ca/**")                  //选课信息相关
                .addPathPatterns("/api/course/createCourse")    //创建课程
                .addPathPatterns("/api/course/deleteCourse")    //删除
                .addPathPatterns("/api/course/modifiCourse")    //修改
                .addPathPatterns("/api/lesson/**")              //课时修改,创建,删除
                .addPathPatterns("/api/live/pushend")           //直播结束
                .excludePathPatterns("/api/lesson/get")
        ;
    }

    private CorsConfiguration buildConfig() {
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        corsConfiguration.addAllowedOrigin("*");
        corsConfiguration.addAllowedHeader("*");
        corsConfiguration.addAllowedMethod("*");
        return corsConfiguration;
    }

    @Bean
    public CorsFilter corsFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", buildConfig());
        return new CorsFilter(source);
    }
}
